%% Load dll
%%% ETTAPIConnector 1.1 (2021-03-18)
% Close all instances of Matlab to update
% Start order:
% 0. Copy the ETTAPI.dll into your desired location
% 1. Open ETTDirectControl on PC with device (Olfactometer/Gustometer)
%       connected
% 1.a. ETTDirectControl allows to automatically establish a connection via 
%       Settings --> Experimental --> "Automatically run Matlab Connector" 
% 1.b. For the setting to be "activated" the ETTDirectControl will need to
%       be restarted
% 1.c. There is a "Manual (Re)-Start" button in the Matlab Connector
% section to initiate communication between the two programs manually or
% after the client was stopped
% 2. Confirm the "Server started" is "true" 
% 3. Execute the script below
% 
% Notes:
% Any function CAN be handed a deviceID, which is the unique ID that can be retrieved from
% ScanDeviceList()
% Any function will always default to the first device that is connected to
% ETTDirectControl if no index is handed in (useful if only one device is
% utilized)
if exist('Device','var') && Device.IsConnected==0 
    disp('Device is initialized')
    return
end
clc
% clear all
%% Connect to library and local/remote PC running ETTDC
dllpath='C:\\Program Files\ETTDirectControl\ETTAPI\ETTAPI.dll';%
host='127.0.0.1';% 127.0.0.1 for local host (same computer)
lib=NET.addAssembly(dllpath); % load DLL (caution DLL can't be unloaded/overwritten until Matlab is closed again)
%% assign object
Device=ETTAPI.ETTDeviceAPI;% initialize Device object

pause(1) % allow for some COM overhead
disp(sprintf('Library Version: %s', Device.Version)); % print version

%% establish connection
% can be veryfied in ETTDirectControl->Settings->Experimental
% if a remote host is used make sure that the computer is connectable via
% cmd ping hostname
if Device.Connect(host)% connects to local/remote device
	disp('Connection to ETTDirectControl established');
else
	clear Device
    disp('Connection to ETTDirectControl failed');
	
end
